/*
 * Copyright (c) 2010-2020 Belledonne Communications SARL.
 *
 * This file is part of linphone-android
 * (see https://www.linphone.org).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.newlink.building.activities

import android.os.Bundle
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.navigation.NavController
import androidx.navigation.NavOptions
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.fragment.findNavController
import androidx.slidingpanelayout.widget.SlidingPaneLayout
import com.newlink.building.R
import com.newlink.building.activities.main.Module_Phone_LinphoneMainActivity
import com.newlink.building.activities.main.fragments.Module_Phone_TabsFragment
import com.newlink.building.activities.main.settings.fragments.*

internal fun Fragment.findMasterNavController(): NavController {
    return parentFragment?.parentFragment?.findNavController() ?: findNavController()
}

fun popupTo(
    popUpTo: Int = -1,
    popUpInclusive: Boolean = false,
    singleTop: Boolean = true
): NavOptions {
    val builder = NavOptions.Builder()
    builder.setPopUpTo(popUpTo, popUpInclusive).setLaunchSingleTop(singleTop)
    return builder.build()
}

/* Main activity related */
internal fun Module_Phone_LinphoneMainActivity.navigateToDialer(args: Bundle?) {
    findNavController(R.id.nav_host_fragment).navigate(
        R.id.action_global_dialerFragment,
        args,
        popupTo(R.id.dialerFragment, true)
    )
}

internal fun Module_Phone_LinphoneMainActivity.navigateToSettings(args: Bundle?) {
    findNavController(R.id.nav_host_fragment).navigate(
        R.id.action_global_settingsFragment,
        args,
        popupTo(R.id.settingsFragment, true)
    )
}

internal fun Module_Phone_TabsFragment.navigateToDialer() {
    val action = when (findNavController().currentDestination?.id) {
        else -> R.id.action_global_dialerFragment
    }
    findNavController().navigate(
        action,
        null,
        popupTo(R.id.dialerFragment, true)
    )
}

internal fun Module_Phone_TabsFragment.navigateToSettings() {
    findNavController().navigate(
        R.id.action_global_settingsFragment,
        null,
        popupTo(R.id.settingsFragment, true)
    )
}

internal fun Module_Phone_AboutSettingsFragment.navigateToEmptySetting() {
    navigateToEmptySetting(findNavController())
}

internal fun Module_Phone_AboutSettingsFragment.navigateToUpdateSetting() {
    findNavController().navigate(
        R.id.action_aboutSettingsFragment_to_updateSettingsFragment,
        null,
        popupTo()
    )
}

internal fun navigateToEmptySetting(navController: NavController) {
    navController.navigate(
        R.id.action_global_emptySettingsFragment,
        null,
        popupTo(R.id.emptySettingsFragment, true)
    )
}

internal fun Module_Phone_AboutSettingsFragment.navigateToFeedbackSetting() {
    findNavController().navigate(
        R.id.action_aboutSettingsFragment_to_feedbackSettingsFragment,
        null,
        popupTo()
    )
}

internal fun AccountSettingsFragment.navigateToPhoneLinking(args: Bundle?) {
    if (findNavController().currentDestination?.id == R.id.accountSettingsFragment) {
        findNavController().navigate(
            R.id.action_accountSettingsFragment_to_phoneAccountLinkingFragment,
            args,
            popupTo()
        )
    }
}

internal fun Module_Phone_AdvancedSettingsFragment.navigateToEmptySetting() {
    navigateToEmptySetting(findNavController())
}

internal fun Module_Phone_AudioSettingsFragment.navigateToEmptySetting() {
    navigateToEmptySetting(findNavController())
}

internal fun CallSettingsFragment.navigateToEmptySetting() {
    navigateToEmptySetting(findNavController())
}

internal fun CallSettingsFragment.navigateToAudioCodecSetting(slidingPane: SlidingPaneLayout) {
    if (findNavController().currentDestination?.id == R.id.callSettingsFragment) {

        findNavController().navigate(
            R.id.action_callSettingsFragment_to_audioCodecSettingsFragment,
            null,
            popupTo()
        )
    }
}

internal fun CallSettingsFragment.navigateToVideoCodecSetting(slidingPane: SlidingPaneLayout) {

    if (findNavController().currentDestination?.id == R.id.callSettingsFragment) {

        findNavController().navigate(
            R.id.action_callSettingsFragment_to_videoCodecSettingsFragment,
            null,
            popupTo()
        )
    }
}

internal fun Module_Phone_FeedbackSettingsFragment.navigateToAboutSettings() {
    findNavController().navigate(
        R.id.action_feedbackSettingsFragment_to_aboutSettingsFragment,
        null,
        popupTo()
    )
}


internal fun Module_Phone_LogSettingsFragment.navigateToEmptySetting() {
    navigateToEmptySetting(findNavController())
}

internal fun Module_Phone_TunnelSettingsFragment.navigateToEmptySetting() {
    navigateToEmptySetting(findNavController())
}

internal fun Module_Phone_VideoSettingsFragment.navigateToEmptySetting() {
    navigateToEmptySetting(findNavController())
}

internal fun Module_Phone_AudioCodecSettingsFragment.navigateToEmptySetting() {
    navigateToEmptySetting(findNavController())
}


internal fun Module_Phone_VideoCodecSettingsFragment.navigateToEmptySetting() {
    navigateToCallSetting(findNavController())
}

internal fun navigateToCallSetting(navController: NavController) {
    navController.navigate(
        R.id.action_global_callSettingsFragment,
        null,
        popupTo(R.id.callSettingsFragment, true)
    )
}

/* Settings related */

internal fun Module_Phone_SettingsFragment.navigateToAccountSettings(identity: String) {
    if (findNavController().currentDestination?.id == R.id.settingsFragment) {
        val bundle = bundleOf("Identity" to identity)
        val navHostFragment =
            childFragmentManager.findFragmentById(R.id.settings_nav_container) as NavHostFragment
        navHostFragment.navController.navigate(
            R.id.action_global_accountSettingsFragment,
            bundle,
            popupTo(R.id.accountSettingsFragment, true)
        )
    }
}

internal fun Module_Phone_SettingsFragment.navigateToTunnelSettings(slidingPane: SlidingPaneLayout) {
    if (findNavController().currentDestination?.id == R.id.settingsFragment) {
        val navHostFragment =
            childFragmentManager.findFragmentById(R.id.settings_nav_container) as NavHostFragment
        navHostFragment.navController.navigate(
            R.id.action_global_tunnelSettingsFragment,
            null,
            popupTo(R.id.tunnelSettingsFragment, true)
        )
        if (!slidingPane.isOpen) slidingPane.openPane()
    }
}

internal fun Module_Phone_SettingsFragment.navigateToAudioSettings(slidingPane: SlidingPaneLayout) {
    if (findNavController().currentDestination?.id == R.id.settingsFragment) {
        val navHostFragment =
            childFragmentManager.findFragmentById(R.id.settings_nav_container) as NavHostFragment
        navHostFragment.navController.navigate(
            R.id.action_global_audioSettingsFragment,
            null,
            popupTo(R.id.audioSettingsFragment, true)
        )
        if (!slidingPane.isOpen) slidingPane.openPane()
    }
}

internal fun Module_Phone_SettingsFragment.navigateToVideoSettings(slidingPane: SlidingPaneLayout) {
    if (findNavController().currentDestination?.id == R.id.settingsFragment) {
        val navHostFragment =
            childFragmentManager.findFragmentById(R.id.settings_nav_container) as NavHostFragment
        navHostFragment.navController.navigate(
            R.id.action_global_videoSettingsFragment,
            null,
            popupTo(R.id.videoSettingsFragment, true)
        )
        if (!slidingPane.isOpen) slidingPane.openPane()
    }
}

internal fun Module_Phone_SettingsFragment.navigateToCallSettings(slidingPane: SlidingPaneLayout) {
    if (findNavController().currentDestination?.id == R.id.settingsFragment) {
        val navHostFragment =
            childFragmentManager.findFragmentById(R.id.settings_nav_container) as NavHostFragment
        navHostFragment.navController.navigate(
            R.id.action_global_callSettingsFragment,
            null,
            popupTo(R.id.callSettingsFragment, true)
        )
        if (!slidingPane.isOpen) slidingPane.openPane()
    }
}

internal fun Module_Phone_SettingsFragment.navigateToChatSettings(slidingPane: SlidingPaneLayout) {
    if (findNavController().currentDestination?.id == R.id.settingsFragment) {
        val navHostFragment =
            childFragmentManager.findFragmentById(R.id.settings_nav_container) as NavHostFragment
        navHostFragment.navController.navigate(
            R.id.action_global_chatSettingsFragment,
            null,
            popupTo(R.id.chatSettingsFragment, true)
        )
        if (!slidingPane.isOpen) slidingPane.openPane()
    }
}

internal fun Module_Phone_SettingsFragment.navigateToNetworkSettings(slidingPane: SlidingPaneLayout) {
    if (findNavController().currentDestination?.id == R.id.settingsFragment) {
        val navHostFragment =
            childFragmentManager.findFragmentById(R.id.settings_nav_container) as NavHostFragment
        navHostFragment.navController.navigate(
            R.id.action_global_networkSettingsFragment,
            null,
            popupTo(R.id.networkSettingsFragment, true)
        )
        if (!slidingPane.isOpen) slidingPane.openPane()
    }
}

internal fun Module_Phone_SettingsFragment.navigateToUpdateSettings(slidingPane: SlidingPaneLayout) {
    if (findNavController().currentDestination?.id == R.id.settingsFragment) {
        val navHostFragment =
            childFragmentManager.findFragmentById(R.id.settings_nav_container) as NavHostFragment
        navHostFragment.navController.navigate(
            R.id.action_global_updateSettingsFragment,
            null,
            popupTo(R.id.updateSettingsFragment, true)
        )
        if (!slidingPane.isOpen) slidingPane.openPane()
    }
}

internal fun Module_Phone_SettingsFragment.navigateToLogSettings(slidingPane: SlidingPaneLayout) {
    if (findNavController().currentDestination?.id == R.id.settingsFragment) {
        val navHostFragment =
            childFragmentManager.findFragmentById(R.id.settings_nav_container) as NavHostFragment
        navHostFragment.navController.navigate(
            R.id.action_global_logSettingsFragment,
            null,
            popupTo(R.id.logSettingsFragment, true)
        )
        if (!slidingPane.isOpen) slidingPane.openPane()
    }
}

internal fun Module_Phone_SettingsFragment.navigateToContactsSettings(slidingPane: SlidingPaneLayout) {
    if (findNavController().currentDestination?.id == R.id.settingsFragment) {
        val navHostFragment =
            childFragmentManager.findFragmentById(R.id.settings_nav_container) as NavHostFragment
        navHostFragment.navController.navigate(
            R.id.action_global_contactsSettingsFragment,
            null,
            popupTo(R.id.contactsSettingsFragment, true)
        )
        if (!slidingPane.isOpen) slidingPane.openPane()
    }
}

internal fun Module_Phone_SettingsFragment.navigateToAdvancedSettings(slidingPane: SlidingPaneLayout) {
    if (findNavController().currentDestination?.id == R.id.settingsFragment) {
        val navHostFragment =
            childFragmentManager.findFragmentById(R.id.settings_nav_container) as NavHostFragment
        navHostFragment.navController.navigate(
            R.id.action_global_advancedSettingsFragment,
            null,
            popupTo(R.id.advancedSettingsFragment, true)
        )
        if (!slidingPane.isOpen) slidingPane.openPane()
    }
}